package cn.lili.modules.system.serviceimpl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollectionUtil;
import cn.lili.common.exception.ServiceException;
import cn.lili.common.utils.ResultUtil;
import cn.lili.common.utils.StringUtils;
import cn.lili.common.vo.PageVO;
import cn.lili.common.vo.ResultMessage;
import cn.lili.modules.goods.entity.enums.GoodsTypeEnum;
import cn.lili.modules.system.client.DictionaryClient;
import cn.lili.modules.system.constants.DictConstant;
import cn.lili.modules.system.entity.dos.Dictionary;
import cn.lili.modules.system.entity.dos.PackConfig;
import cn.lili.modules.system.entity.vo.PackConfigVO;
import cn.lili.modules.system.mapper.PackConfigMapper;
import cn.lili.modules.system.service.PackConfigService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * @author asus
 * @description 针对表【li_pack_config】的数据库操作Service实现
 * @createDate 2024-03-21 15:57:19
 */
@Service
public class PackConfigServiceImpl extends ServiceImpl<PackConfigMapper, PackConfig>
        implements PackConfigService {

    @Resource
    private DictionaryClient dictionaryClient;

    /**
     * 礼包配置分页列表
     * @param pageVO
     * @param packConfigVO
     * @return
     */
    @Override
    public ResultMessage<Object> pageVO(PageVO pageVO, PackConfigVO packConfigVO) {
        Page<PackConfig> configPage = super.page(new Page<>(pageVO.getPageNumber(), pageVO.getPageSize()), this.createWrapper(packConfigVO));
        return ResultUtil.data(this.conversion(configPage));
    }

    /**
     * 保存数据的前置校验
     * @param packConfig
     */
    @Override
    public void saveCheck(PackConfig packConfig) {
        if (StringUtils.isNotEmpty(packConfig.getId())) {
            throw new ServiceException("礼包Id需为空");
        }
        if (StringUtils.isEmpty(packConfig.getPackName())) {
            throw new ServiceException("礼包名称为空");
        }
        if (StringUtils.isEmpty(packConfig.getDictId())) {
            throw new ServiceException("未选择字典");
        }
        //没选择，默认是可用状态
        if (packConfig.getPackStatus() == null) {
            packConfig.setPackStatus(DictConstant.STATUS_1);
        }
    }

    /**
     * 修改数据的前置校验
     * @param packConfig
     */
    @Override
    public void updateCheck(PackConfig packConfig) {
        if (StringUtils.isEmpty(packConfig.getId())) {
            throw new ServiceException("id为空");
        }
    }

    /**
     * 数据转换
     * @param configPage
     * @return
     */
    private Page<PackConfigVO> conversion(Page<PackConfig> configPage) {
        if (CollectionUtil.isEmpty(configPage.getRecords())) {
            return new Page<>();
        }
        List<Dictionary> dictionaryList = dictionaryClient.getListByCode(DictConstant.PACK_CONFIG_NAME);

        Page<PackConfigVO> result = new Page<>();
        BeanUtil.copyProperties(configPage, result);
        List<PackConfigVO> recordVOList = new ArrayList<>();
        for (PackConfig packConfig : configPage.getRecords()) {
            PackConfigVO packConfigVO = BeanUtil.toBean(packConfig, PackConfigVO.class);
            //plus会员时长
            if (!dictionaryList.isEmpty()) {
                for (Dictionary dictionary : dictionaryList) {
                    if (dictionary.getId().equals(packConfigVO.getDictId())) {
                        packConfigVO.setDictName(dictionary.getValueName());
                    }
                }
            }
            //是否发货
            packConfigVO.setIsDeliverName("否");
            if (packConfigVO.getIsDeliver() != null && packConfigVO.getIsDeliver()) {
                packConfigVO.setIsDeliverName("是");
            }
            //可用状态
            if (packConfigVO.getPackStatus()!=null) {
                if (packConfigVO.getPackStatus().equals(1)) {
                    packConfigVO.setPackStatusName("启用");
                } else {
                    packConfigVO.setPackStatusName("禁用");
                }
            }
            //类型：虚拟/实物  GoodsTypeEnum
            if (StringUtils.isNotEmpty(packConfigVO.getPackType())) {
                if (GoodsTypeEnum.PHYSICAL_GOODS.name().equals(packConfigVO.getPackType())) {
                    packConfigVO.setPackTypeName(GoodsTypeEnum.PHYSICAL_GOODS.description());
                }
            }
            recordVOList.add(packConfigVO);
        }
        result.setRecords(recordVOList);
        return result;
    }

    /**
     * 构建查询条件
     *
     * @return 查询条件
     */
    private LambdaQueryWrapper<PackConfig> createWrapper(PackConfigVO packConfigVO) {
        LambdaQueryWrapper<PackConfig> configLambdaQueryWrapper = new LambdaQueryWrapper<>();
        configLambdaQueryWrapper.eq(packConfigVO.getPackStatus() != null, PackConfig::getPackStatus, packConfigVO.getPackStatus());
        configLambdaQueryWrapper.orderByDesc(PackConfig::getCreateTime);
        return configLambdaQueryWrapper;
    }

    /**
     * 根据packids查询信息
     * @param packIds
     * @return
     */
    @Override
    public List<PackConfig> getPackConfigByPackIds(List<String> packIds) {
        LambdaQueryWrapper<PackConfig> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.in(PackConfig::getId, packIds);
        // 状态，0禁用 1启用
        queryWrapper.eq(PackConfig::getPackStatus, 1);
        return this.list(queryWrapper);
    }
}




